package com.web.comment.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ModelDriven;
import com.util.BaseActionUtil;
import com.util.CommonUtil;
import com.util.JsonUtil;
import com.web.comment.dao.CommentDAO;
import com.web.comment.po.CommentPo;
import com.web.comment.service.CommentService;
import com.web.system.po.RolePo;
import com.web.system.po.UserPo;

@Controller @Scope("prototype") 
@ParentPackage(value = "struts-default")
@Namespace(value = "/")
@Action(value = "commentAction",results={

})
public class CommentAction extends BaseActionUtil implements ModelDriven<CommentPo> {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = -3744759973330506373L;

	@Autowired
	private CommentService commentService;

	private CommentPo comment;
	
	private String commentIds;
	
	public CommentPo getModel() {
		// TODO Auto-generated method stub
		comment = new CommentPo();
		return comment;
	}

	public String getCommentList() throws Exception {
		//分页部分
		int page = Integer.parseInt(request.getParameter("page"));
		int pageSize = Integer.parseInt(request.getParameter("rows"));
		int first = pageSize * (page - 1);
		
		//搜索部分
		String sql = "";
		
		String commentContext = request.getParameter("commentContext");
		String hotelNameSearch = request.getParameter("hotelNameSearch");
		String userNameSearch = request.getParameter("userNameSearch");
		String date_from = request.getParameter("date_from");
		String date_to = request.getParameter("date_to");
		if(CommonUtil.isNotNull(userNameSearch)){
			sql += " and c.user.userName like '%"+userNameSearch+"%'";
		}
		if(CommonUtil.isNotNull(hotelNameSearch)){
			sql += " and c.hotel.hotelName like '%"+hotelNameSearch+"%'";
		}
		if(CommonUtil.isNotNull(commentContext)){
			sql += " and c.commentContext like '%"+commentContext+"%'";
		}
		if(CommonUtil.isNotNull(date_from)){
			sql += " and c.createTime >= '" + date_from + "'";
		}
		if(CommonUtil.isNotNull(date_to)){
			sql +=" and c.createTime <= '" + date_to + "'";
		}
		
		//组合最终sql语句
		StringBuffer queryStringBuffer = new StringBuffer();
		queryStringBuffer.append("FROM CommentPo as c");
		queryStringBuffer.append(" left outer join fetch c.user as u");
		queryStringBuffer.append(" left outer join fetch c.hotel as h");
		queryStringBuffer.append(" where 1=1 ");
		queryStringBuffer.append(sql);
		queryStringBuffer.append(" ORDER BY c.hotel.hotelId , c.commentId desc");
		
        List<CommentPo> comments = commentService.getCommentList(queryStringBuffer.toString(), first, pageSize);
        List<Map<String, Object>> commentList = new ArrayList<Map<String, Object>>();
        if(comments != null && comments.size() > 0){
        	  for (int i = 0; i < comments.size(); i++) {
              	Map<String, Object> map = new HashMap<String, Object>();
              	map.put("userId", comments.get(i).getUser().getUserId());
              	map.put("userName", comments.get(i).getUser().getUserName());
              	map.put("commentId", comments.get(i).getCommentId());
              	map.put("hotelId", comments.get(i).getHotel().getHotelId());
              	map.put("hotelName", comments.get(i).getHotel().getHotelName());
              	map.put("commentContext", comments.get(i).getCommentContext());
              	map.put("createTime", comments.get(i).getCreateTime());
              	commentList.add(map);
      		}
        }
      
        Map<String, Object> jsonMap = new HashMap<String, Object>();
        int total = commentService.total(sql);
        jsonMap.put("total", total);
        jsonMap.put("rows", commentList);
        out.print(JsonUtil.objectToStr(jsonMap));
        out.flush();
        out.close();
		return NONE;
	}
	
	
	public void deleteComments()throws Exception {
		if(CommonUtil.isNotNull(commentIds)){
			out.print(commentService.deleteComments(commentIds));
		} else{
			out.print("-1");
		}
		out.flush();
		out.close();
	}
	
	public void getCommentByCommentId() throws Exception {
	   String id = request.getParameter("commentId");
	   Map<String, Object> map = new HashMap<String, Object>();
	   if(CommonUtil.isNotNull(id)){
		   comment = commentService.getCommentByCommentId(Integer.parseInt(id));
		   if(comment != null){
			   map.put("hotelName", comment.getHotel().getHotelName());
			   map.put("userName", comment.getUser().getUserName());
			   map.put("createTime", comment.getCreateTime());
			   map.put("commentContext", comment.getCommentContext());
		   }
	   }
	   //mapJson.put("comment", map);
	   out.print(JsonUtil.objectToStr(map));
	   out.flush();
	   out.close();
	}
	
	public String execute() throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	public String getCommentIds() {
		return commentIds;
	}

	public void setCommentIds(String commentIds) {
		this.commentIds = commentIds;
	}
	
}
